package com.ls.atf.core.web.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.ls.atf.core.web.WebPage;
import com.ls.atf.core.web.elements.CustomElement;
import com.ls.atf.core.web.elements.Link;
import com.ls.atf.data.UserData;

public class MailInboxPage extends WebPage<MailInboxPage> {
	
	private static final String LOGGEDIN_USER_XPATH_LOCATOR1 = "//span[contains(text(), '";
	private static final String LOGGEDIN_USER_XPATH_LOCATOR2 = "')]";
	private static final String INBOX_XPATH_LOCATOR = "//a[contains(text(), 'Inbox')]";
	private static final String MAIL_XPATH_LOCATOR1 = "//span[@email='";
	private static final String MAIL_XPATH_LOCATOR2 = "']";

	private UserData user;
	private String email;

	public MailInboxPage(WebDriver driver, UserData user, String email) {
		super(driver);
		this.user = user;
		this.email = email;
	}

	@Override
	public MailInboxPage load() {
		return this;
	}

	@Override
	public boolean isAvailable() {
		return getLoggedInUser().isAvailable() &&
				getInboxLink().isAvailable() &&
				getMail().waitUntilAvailable().isAvailable();
	}
	
	private CustomElement getLoggedInUser() {
		return new CustomElement(driver, By.xpath(LOGGEDIN_USER_XPATH_LOCATOR1 + user.getEmail() + LOGGEDIN_USER_XPATH_LOCATOR2));
	}

	private Link getInboxLink() {
		return new Link(driver, By.xpath(INBOX_XPATH_LOCATOR));
	}
	
	private Link getMail() {
		return new Link(driver, By.xpath(MAIL_XPATH_LOCATOR1 + email + MAIL_XPATH_LOCATOR2));
	}

	public void openConfirmationEmail() {
		getMail().click();
	}

}
